package gui;

import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JTextField;

import controllers.OBLSettings;
import controllers.SubscriberUIP;

import Entity.User;

import messagesPacks.BookSubjectMessage;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Enumeration;
import javax.swing.SwingConstants;
import javax.swing.JList;
import java.awt.SystemColor;
import javax.swing.border.BevelBorder;
import javax.swing.ImageIcon;
/**
 * This class representing a Subscriber Gui
 * Date: 25/12/2012
 * @author Roman
 *
 */
public class SubscriberUI extends JPanel 
{	
	private static final long serialVersionUID = 1L;
	
	JButton btnLogout;
	JButton btnExit;
	JButton btnEditProfile;
	JButton btnViewHistory;
	JButton btnSearchABook;
	JButton btnExtendBorrow;
	
	JLabel lblOblSubscriber;
	JLabel lblWelcome;
	JLabel SuscriberNamelabel;
	private JSeparator separator;
	private JSeparator separator_1;
	
	SubscriberEditProfileUI subEditProfilePanel  ;
	SubscriberSearchBookUI  subSearchBookPanel  ;
	SubscriberViewHistoryUI subViewHistoPanel  ;
	SubscriberExtendBorrowUI subExtendPanel ; 
	SubscriberUIP subscriberUIP;
	
    User subscriber = null ; 
    private JLabel label_background1;
    private JLabel label_background2;
    private JLabel All_background_label;
    
	public SubscriberUI() 
	{
		super();
		this.subscriberUIP = new SubscriberUIP(); 
		initialize();
	}
	/**
	 * This method initializes LoginPanel
	 */
	private void initialize()
	{
		setLayout(null);
		this.setSize(OBLSettings.pane2Size[0],OBLSettings.pane2Size[1]);
		separator = new JSeparator();
		separator.setBounds(-18, 57, 837, 20);
		add(separator);
		
		lblOblSubscriber = new JLabel("OBL Subscriber");
		lblOblSubscriber.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblOblSubscriber.setBounds(310, 11, 179, 35);
		add(lblOblSubscriber);
		
		btnLogout = new JButton("Logout");

		btnLogout.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnLogout.setBounds(614, 11, 83, 41);
		add(btnLogout);
		
		btnExit = new JButton("Exit");
		btnExit.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnExit.setBounds(707, 11, 83, 41);
		add(btnExit);
		
		btnSearchABook = new JButton("Search a book");
		btnSearchABook.setBorder(null);
		btnSearchABook.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnSearchABook.setBounds(27, 354, 118, 41);
		add(btnSearchABook);
		
		btnViewHistory = new JButton("View history");
		btnViewHistory.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnViewHistory.setBounds(27, 171, 118, 41);
		add(btnViewHistory);
		
		btnEditProfile = new JButton("Edit profile");
		btnEditProfile.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnEditProfile.setBounds(27, 88, 118, 41);
		add(btnEditProfile);
		
		SuscriberNamelabel = new JLabel("");
		SuscriberNamelabel.setFont(new Font("Trebuchet MS", Font.ITALIC, 20));
		SuscriberNamelabel.setBounds(10, 11, 267, 35);
		add(SuscriberNamelabel);
		
		separator_1 = new JSeparator();
		separator_1.setBounds(-15, 512, 837, 35);
		add(separator_1);

		this.subSearchBookPanel = new SubscriberSearchBookUI(getSubscriberUi());
		
	    BookSubjectMessage BookSubject_msg ;
	    BookSubject_msg = subscriberUIP.getSubjectsOfBooks();
	    
	 //load the books subjects in to the combobox 
	    this.subSearchBookPanel.BookSubjects.put(0,"Please select subject...");
	    this.subSearchBookPanel.BookSubjects.putAll( BookSubject_msg.getBookSubjects());
	    
	    Enumeration e1 = BookSubject_msg.getBookSubjects().elements();
	    this.subSearchBookPanel.subjectcomboBox.addItem("Please select subject...");
	    while ( e1.hasMoreElements() )
	    	this.subSearchBookPanel.subjectcomboBox.addItem( (String)e1.nextElement() );
	    	
	   
	    this.subViewHistoPanel = new SubscriberViewHistoryUI(getSubscriberUi());
	    this.subEditProfilePanel = new SubscriberEditProfileUI(getSubscriberUi());
	    this.subExtendPanel = new SubscriberExtendBorrowUI(getSubscriberUi());
	    
	    lblWelcome = new JLabel("  ");
	    lblWelcome.setFont(new Font("Arial Unicode MS", Font.BOLD, 16));
	    lblWelcome.setBounds(214, 63, 250, 23);
	    add(lblWelcome);
	    
	    btnExtendBorrow = new JButton("Extend Borrow");
	    btnExtendBorrow.setBounds(27, 265, 118, 41);
	    add(btnExtendBorrow);
	    
	    JSeparator separator_2 = new JSeparator();
	    separator_2.setOrientation(SwingConstants.VERTICAL);
	    separator_2.setBounds(185, 58, 19, 454);
	    add(separator_2);
	    
	    label_background1 = new JLabel("");
	    label_background1.setIcon(OBLSettings.IMG_SUBSCRIBERUI_BACKGROUND2);
	    label_background1.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
	    label_background1.setBounds(10, 63, 153, 438);
	    add(label_background1);
	    
	    label_background2 = new JLabel("");
	    label_background2.setIcon(OBLSettings.IMG_SUBSCRIBERUI_BACKGROUND1);
	    label_background2.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
	    label_background2.setBounds(206, 63, 584, 438);
	    add(label_background2);
	    
	    All_background_label = new JLabel("");
	    All_background_label.setIcon(new ImageIcon(""));
	    All_background_label.setBounds(0, 59, 800, 452);
	    add(All_background_label);
//================================================================		
//                      Action Listeners
//================================================================	
		
		//searchBook button
		btnSearchABook.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				
				WelcomeUI.wui.switchPanel("OBL System - Subscriber",subSearchBookPanel ,OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);
				subSearchBookPanel.searchByBookNameFiled.grabFocus();
				
			}
		});
		
		//viewHistory button
		btnViewHistory.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				WelcomeUI.wui.switchPanel("OBL System - Subscriber",subViewHistoPanel ,OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);
				subViewHistoPanel.showHistory(subscriber.getID());
			}
		});
		
		//editProfile button
		btnEditProfile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				subEditProfilePanel.EmailTextField.setText( subscriber.getEmail()); 
				subEditProfilePanel.AddressTextField.setText( subscriber.getAddress());
				subEditProfilePanel.phoneTextField.setText( subscriber.getPhone());   
				WelcomeUI.wui.switchPanel("OBL System - Subscriber",subEditProfilePanel ,OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);
			}
		});
		//extendBorrows button
	    btnExtendBorrow.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent arg0)
	    	{
	    		
				WelcomeUI.wui.switchPanel("OBL System - Subscriber",subExtendPanel ,OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);
				subExtendPanel.showCurrentBorrows(subscriber.getID());
	    		
	    	}
	    });
		
	}
	
	public SubscriberUI getSubscriberUi()
	{
		return this;
	}
	
	
	public void paintComponent(Graphics g) 
	{
		g.drawImage((OBLSettings.IMG_DEFAULT_BACKGROUND).getImage(), 0, 0, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1],null);
		
	}
}
